﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lite;
using System.Data.SqlClient;
using lite.sqlserver;
using System.Data;
using System.Collections;

namespace OpenExpressApp
{
    public static class Extensions
    {
        public static IGTable QueryTable(this IDb db, string sql)
        {
            using (var adapter = new SqlDataAdapter(sql, db.Connection as SqlConnection))
            {
                var table = new DataTable();
                adapter.Fill(table);

                SqlTable.Trace(sql);

                return new GTable(table);
            }
        }

        public static IResultSet ExecSql(this IDb db, string sql)
        {
            return db.Exec("sys.sp_sqlexec", new object[] { sql });
        }

        public static IList ExecSql(this IDb db, Type type, string sql)
        {
            return db.Exec(type, "sys.sp_sqlexec", new object[] { sql });
        }

        public static List<TEntity> ExecSql<TEntity>(this IDb db, string sql)
        {
            return ExecSql(db, typeof(TEntity), sql).Cast<TEntity>().ToList();
        }

        public static IList<T> Select<T>(this IDb db)
        {
            return db.Select<T>(db.Query());
        }
    }
}
